//
//  LCGPDetailViewController.m
//  YinYueTai
//
//  Created by CongCong on 15-7-23.
//  Copyright (c) 2015年 CongCong. All rights reserved.
//

#import "LCGPDetailViewController.h"

@interface LCGPDetailViewController ()
@property (nonatomic) LCGPDetail *detailModel;
@property (nonatomic) NSMutableArray *sizeArray;
@property (nonatomic, copy) NSNumber *total;
@end

@implementation LCGPDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.sizeArray = [NSMutableArray array];
    [self creatCollection];
    [self registerNib];
    [self creatRefresh];
    [self fetchWebDataWith:kGPDetailHeader body:[NSString stringWithFormat:kGPDetailBody,self.contentID]];
    
}

- (void)initJosnModelWith:(NSDictionary *)dataDict
{
    NSDictionary *dict = dataDict[@"data"];
    if (self.page) {
        for (NSDictionary *listDict in dict[@"list"]) {
            LCGPDetailListModel *listModel = [[LCGPDetailListModel alloc]initWithDictionary:listDict error:nil];
            [self.detailModel.commentlist addObject:listModel];
        }
    }
    else
    {
        self.detailModel = [[LCGPDetail alloc]initWithDictionary:dict error:nil];
        
    }
    [self.sizeArray removeAllObjects];
    for (int i=0; i<self.detailModel.commentlist.count; i++) {
        LCGPDetailListModel *model = self.detailModel.commentlist[i];
        NSString *content = model.content;
        CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(kScreenSize.width-20, 10000.f)];
        NSValue *value = [NSValue valueWithCGSize:size];
        [self.sizeArray addObject:value];
    }
    [self.collectionView reloadData];
}
- (void)registerNib
{
    self.flowLayout.minimumLineSpacing = 10;
    self.flowLayout.minimumInteritemSpacing = 10;
    self.collectionView.frame = CGRectMake(0, 0, kScreenSize.width, kScreenSize.height);
    [self.collectionView registerNib:[UINib nibWithNibName:@"LCGPDetailCell" bundle:nil] forCellWithReuseIdentifier:@"LCGPDetailCell"];
}

- (void)creatRefresh
{
    __weak typeof(self) weakSelf = self;
    [self.collectionView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page = 0;
        [weakSelf fetchWebDataWith:kGPDetailHeader body:[NSString stringWithFormat:kGPDetailBody,weakSelf.contentID]];
    }];
    [self.collectionView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page+=10;
        if (weakSelf.page>=weakSelf.detailModel.commenttotal.integerValue) {
            [MMProgressHUD show];
            [MMProgressHUD  dismissWithSuccess:@"到底了..."];
            [weakSelf.collectionView footerEndRefreshing];
            return ;
        }
        else
        {
            [weakSelf fetchWebDataWith:kGPDetailMoreHeader body:[NSString stringWithFormat:kGPDetailMoreBody,weakSelf.contentID,weakSelf.page]];
        }
    }];

}
#pragma mark - <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.detailModel.commentlist.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCGPDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCGPDetailCell" forIndexPath:indexPath];
    LCGPDetailListModel *modle = self.detailModel.commentlist[indexPath.row];
    NSValue *value = self.sizeArray[indexPath.row];
    CGSize size = value.CGSizeValue;
    [cell showModel:modle andSize:size];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSValue *value = self.sizeArray[indexPath.row];
    CGSize size = value.CGSizeValue;
    return CGSizeMake(kScreenSize.width-10, size.height+50);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //点击事件
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *header;
    NSString *content = self.content;
    CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(kScreenSize.width-20, 10000.f)];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, kScreenSize.width-20, size.height+40)];
    label.numberOfLines = 0;
    label.tag = 1001;
    if([kind isEqual:UICollectionElementKindSectionHeader])
    {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        UILabel *oldlabel = (UILabel*)[header viewWithTag:1001];
        [oldlabel removeFromSuperview];
        [header addSubview:label];
    }
    label.text = [NSString stringWithFormat:@"%@\n%@",self.myTitle,self.content];
    return header;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    NSString *content = self.content;
    CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(kScreenSize.width-20, 10000.f)];

    return CGSizeMake(kScreenSize.width, size.height+40);
}


@end
